dragicon: Change how to acquire drag icons
authorBenjamin Otte <otte@redhat.com>
Mon, 2 Mar 2020 01:55:38 +0000 (02:55 +0100)
committerBenjamin Otte <otte@redhat.com>
Mon, 2 Mar 2020 03:43:56 +0000 (04:43 +0100)
Before, gtk_drag_icon_new_for_drag() allowed creating new drag icons.
This could cause multiple drag icons to exist for a single drag.

Now, gtk_drag_icon_get_for_drag() makes sure that only one drag icon is
created.

docs/reference/gtk/gtk4-sections.txt
gtk/gtkdragicon.c
gtk/gtkdragicon.h
gtk/gtkdragiconprivate.h [deleted file]
gtk/gtkdragsource.c

index 769926af477c3430aa2f3a9a7689a387cc42e4df..419b28193c6e46f978f90ead17f89318355e8506 100644 (file)
@@ -6880,7 +6880,7 @@ gtk_drop_controller_motion_get_type
 <SECTION>
 <FILE>gtkdragicon</FILE>
 GtkDragIcon
-gtk_drag_icon_new_for_drag
+gtk_drag_icon_get_for_drag
 gtk_drag_icon_set_child
 gtk_drag_icon_get_child
 gtk_drag_icon_set_from_paintable
index c217ab6b92201995f7ab7d1e80bddecc90739386..4e1afa522aeb18ddba73fa08110eec387ee1dbea 100644 (file)
@@ -17,7 +17,7 @@
 
 #include "config.h"
 
-#include "gtkdragiconprivate.h"
+#include "gtkdragicon.h"
 
 #include "gtkprivate.h"
 #include "gtkintl.h"
  * @Short_description: A toplevel to use as drag icon
  * @Title: GtkDragIcon
  *
- * GtkDragIcon is a #GtkNative implementation with the sole purpose
+ * GtkDragIcon is a #GtkRoot implementation with the sole purpose
  * to serve as a drag icon during DND operations. A drag icon moves
  * with the pointer during a drag operation and is destroyed when
  * the drag ends.
  *
  * To set up a drag icon and associate it with an ongoing drag operation,
- * use gtk_drag_icon_set_from_paintable(). It is also possible to create
- * a GtkDragIcon with gtk_drag_icon_new_for_drag(() and populate it
- * with widgets yourself.
+ * use gtk_drag_icon_get_for_drag() to get the icon for a drag. You can
+ * then use it like any other widget and use gtk_drag_icon_set_child() to
+ * set whatever widget should be used for the drag icon.
+ *
+ * Keep in mind that drag icons do not allow user input.
  */
 struct _GtkDragIcon
 {
@@ -385,32 +387,41 @@ gtk_drag_icon_init (GtkDragIcon *self)
 {
 }
 
-GtkWidget *
-gtk_drag_icon_new (void)
-{
-  return g_object_new (GTK_TYPE_DRAG_ICON, NULL);
-}
-
 /**
- * gtk_drag_icon_new_for_drag:
- * @drag: a #GtkDrag
+ * gtk_drag_icon_get_for_drag:
+ * @drag: a #GdkDrag
+ *
+ * Gets the #GtkDragIcon in use with @drag.
  *
- * Creates a #GtkDragIcon and associates it with the drag operation.
+ * If no drag icon exists yet, a new one will be created
+ * and shown.
  *
- * Returns: the new #GtkDragIcon
+ * Returns: (transfer none) the #GtkDragIcon
  */
 GtkWidget *
-gtk_drag_icon_new_for_drag (GdkDrag *drag)
+gtk_drag_icon_get_for_drag (GdkDrag *drag)
 {
-  GtkWidget *icon;
+  static GQuark drag_icon_quark = 0;
+  GtkWidget *self;
 
   g_return_val_if_fail (GDK_IS_DRAG (drag), NULL);
 
-  icon = g_object_new (GTK_TYPE_DRAG_ICON, NULL);
+  if (G_UNLIKELY (drag_icon_quark == 0))
+    drag_icon_quark = g_quark_from_static_string ("-gtk-drag-icon");
+
+  self = g_object_get_qdata (G_OBJECT (drag), drag_icon_quark);
+  if (self == NULL)
+    {
+      self = g_object_new (GTK_TYPE_DRAG_ICON, NULL);
+
+      GTK_DRAG_ICON (self)->surface = g_object_ref (gdk_drag_get_drag_surface (drag));
 
-  gtk_drag_icon_set_surface (GTK_DRAG_ICON (icon), gdk_drag_get_drag_surface (drag));
+      g_object_set_qdata_full (G_OBJECT (drag), drag_icon_quark, g_object_ref_sink (self), g_object_unref);
 
-  return icon;
+      gtk_widget_show (self);
+    }
+
+  return self;
 }
 
 /**
@@ -435,24 +446,11 @@ gtk_drag_icon_set_from_paintable (GdkDrag      *drag,
 
   gdk_drag_set_hotspot (drag, hot_x, hot_y);
 
-  icon = gtk_drag_icon_new_for_drag (drag);
+  icon = gtk_drag_icon_get_for_drag (drag);
 
   picture = gtk_picture_new_for_paintable (paintable);
   gtk_picture_set_can_shrink (GTK_PICTURE (picture), FALSE);
   gtk_drag_icon_set_child (GTK_DRAG_ICON (icon), picture);
-
-  g_object_set_data_full (G_OBJECT (drag),
-                          "icon",
-                          g_object_ref_sink (icon),
-                          (GDestroyNotify)gtk_widget_destroy);
-  gtk_widget_show (icon);
-}
-
-void
-gtk_drag_icon_set_surface (GtkDragIcon *icon,
-                           GdkSurface  *surface)
-{
-  g_set_object (&icon->surface, surface);
 }
 
 /**
index 873e5649425dc10681aff3e691c7b778bc76abe5..b8c70d6f869d39be68af89f46f3a521ff54b0084 100644 (file)
@@ -38,7 +38,7 @@ GDK_AVAILABLE_IN_ALL
 G_DECLARE_FINAL_TYPE (GtkDragIcon, gtk_drag_icon, GTK, DRAG_ICON, GtkContainer)
 
 GDK_AVAILABLE_IN_ALL
-GtkWidget *     gtk_drag_icon_new_for_drag (GdkDrag *drag);
+GtkWidget *     gtk_drag_icon_get_for_drag                      (GdkDrag                *drag);
 
 GDK_AVAILABLE_IN_ALL
 void            gtk_drag_icon_set_child                         (GtkDragIcon            *self,
diff --git a/gtk/gtkdragiconprivate.h b/gtk/gtkdragiconprivate.h
deleted file mode 100644 (file)
index c0fd36a..0000000
+++ /dev/null
@@ -1,42 +0,0 @@
-/* GTK - The GIMP Toolkit
- * Copyright 2019 Matthias Clasen
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library. If not, see <http://www.gnu.org/licenses/>.
- */
-
-/*
- * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
- * file for a list of people on the GTK+ Team.  See the ChangeLog
- * files for a list of changes.  These files are distributed with
- * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
- */
-
-#ifndef __GTK_DRAG_ICON_PRIVATE_H__
-#define __GTK_DRAG_ICON_PRIVATE_H__
-
-#include <gio/gio.h>
-#include <gtk/gtkdragicon.h>
-
-G_BEGIN_DECLS
-
-GtkWidget *     gtk_drag_icon_new                          (void);
-
-void            gtk_drag_icon_set_surface                  (GtkDragIcon *icon,
-                                                            GdkSurface  *surface);
-void            gtk_drag_icon_set_widget                   (GtkDragIcon *icon,
-                                                            GtkWidget   *widget);
-
-G_END_DECLS
-
-#endif /* __GTK_DRAG_ICON_PRIVATE_H__ */
index 155c7cc84a12b332ae92a00f63ab1dc7674f9b2a..6b6c4d38de094977b734b6b17ea30da496e9ec5f 100644 (file)
@@ -34,7 +34,7 @@
 #include "gtkintl.h"
 #include "gtkstylecontext.h"
 #include "gtkimageprivate.h"
-#include "gtkdragiconprivate.h"
+#include "gtkdragicon.h"
 #include "gtkprivate.h"
 #include "gtkmarshalers.h"
 #include "gtkicontheme.h"